package com.woniuxy.moneyapp.app.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.commons.exception.MyException;
import com.woniuxy.commons.util.BeanCopyUtil;
import com.woniuxy.commons.util.Result;
import com.woniuxy.moneyapp.client.YoyaVenueLocationClient;
import com.woniuxy.moneyapp.client.param.*;
import com.woniuxy.moneyapp.common.exception.code.FundExceptionCode;
import com.woniuxy.moneyapp.infra.dto.CoachToParam;
import com.woniuxy.moneyapp.infra.dto.VenueDetails;
import com.woniuxy.moneyapp.infra.dto.VenueLocation;
import com.woniuxy.moneyapp.service.PeopleNearbyService;
import com.woniuxy.signing.client.CocahSigningVenueClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@RestController
public class YoyaVenueLocationContoller implements YoyaVenueLocationClient {

    @Resource
    private PeopleNearbyService pns;

    @Resource
    private StringRedisTemplate rt;

    @Resource
    private CocahSigningVenueClient sigh;

    private ObjectMapper omf = new ObjectMapper();

    //GEO相关命令用到的KEY
    private final static String KEY = "venue_point:";

    //增加 2个
    @Override
    public Result addVenueInRedis(VenueToFund vf) throws JsonProcessingException {
        if(vf==null)
            throw new MyException(FundExceptionCode.PARAM_NULL.getCode(),FundExceptionCode.PARAM_NULL.getMsg());
        //执行储存场馆地理位置的serive
        VenueLocation venueLocation = BeanCopyUtil.copyObject(vf, VenueLocation.class);
        venueLocation.setVenueLa(vf.getVenueLa().doubleValue());
        venueLocation.setVenueLg(vf.getVenueLg().doubleValue());
        if(!pns.saveVenueLocation(venueLocation))
            throw new MyException(FundExceptionCode.REDIS_SAVE_ERROR.getCode(),FundExceptionCode.REDIS_SAVE_ERROR.getMsg());

        //执行储存场馆的信息的操作
        VenueDetails venueDetails = BeanCopyUtil.copyObject(vf, VenueDetails.class);
        venueDetails.setVenueLa(vf.getVenueLa().doubleValue());
        venueDetails.setVenueLg(vf.getVenueLg().doubleValue());

        if(!pns.saveVenueInfo(venueDetails))
            throw new MyException(FundExceptionCode.REDIS_SAVE_ERROR.getCode(),FundExceptionCode.REDIS_SAVE_ERROR.getMsg());
        return new Result().setCode(200).setMsg("储存场馆信息成功");

    }

    @Override
    public Result addCoachInRedis(CoachToFund cf) throws JsonProcessingException {
        if(!pns.saveCoachInfo(BeanCopyUtil.copyObject(cf, CoachToParam.class)))
            throw new MyException(FundExceptionCode.REDIS_SAVE_ERROR.getCode(),FundExceptionCode.REDIS_SAVE_ERROR.getMsg());
        return new Result().setCode(200).setMsg("储存教练信息成功");
    }

    //查
    @Override
    public Result<List<VenueToShow>> VenueAndCoach(FindVenueByCondition vf) throws JsonProcessingException {
        //根据 方法 返回场馆地址对象 id
        List<VenueLocation> venueByCondition = pns.findVenueByCondition(vf.getDistance(), vf.getUserLng(), vf.getUserLat());
        ArrayList<VenueToShow> venueToShows = new ArrayList<>();
        if(venueByCondition.size()==0)
            return new Result<List<VenueToShow>>().setCode(200).setMsg("null").setData(null);
        //通过场馆id 查所有已签约的教练
        for (int i = 0; i <venueByCondition.size() ; i++) {
            VenueLocation venueLocation = venueByCondition.get(i);
            // 从数据库里取
            String s = rt.opsForValue().get("VenueToShowOnMap:" + venueLocation.getId());
            if(s!=null){
                ArrayList<CoachToFund> coachToFunds = new ArrayList<>();
                VenueDetails vd = omf.readValue(s, VenueDetails.class);
                VenueToShow venueToShow = BeanCopyUtil.copyObject(vd, VenueToShow.class);
                Result<List<Integer>> coachListByVenueId = sigh.getCoachListByVenueId(venueToShow.getId());
                if(coachListByVenueId.getData().size()!=0){
                    for (int j = 0; j <coachListByVenueId.getData().size(); j++) {
                        String s2 = rt.opsForValue().get("CoachToShow:" + coachListByVenueId.getData().get(j));
                        if(s2!=null){
                            coachToFunds.add(omf.readValue(s, CoachToFund.class));
                        }
                    }
                }
                venueToShow.setCoaches(coachToFunds);
            }
        }
        return new Result<List<VenueToShow>>().setCode(200).setMsg("查询成功").setData(venueToShows);
    }

    @Override
    public Result renewVenueInfo(VenueToFund vf) throws JsonProcessingException {
        if(vf==null)
            throw new MyException(FundExceptionCode.PARAM_NULL.getCode(),FundExceptionCode.PARAM_NULL.getMsg());
        //删除地理位置接口
        rt.boundZSetOps(KEY).remove(vf.getId()+"");

        //执行储存场馆地理位置的serive
        VenueLocation venueLocation = BeanCopyUtil.copyObject(vf, VenueLocation.class);
        venueLocation.setVenueLa(vf.getVenueLa().doubleValue());
        venueLocation.setVenueLg(vf.getVenueLg().doubleValue());
        if(!pns.saveVenueLocation(venueLocation))
            throw new MyException(FundExceptionCode.REDIS_SAVE_ERROR.getCode(),FundExceptionCode.REDIS_SAVE_ERROR.getMsg());
        //执行储存场馆的信息的操作
        if(!pns.saveVenueInfo(BeanCopyUtil.copyObject(vf, VenueDetails.class)))
            throw new MyException(FundExceptionCode.REDIS_SAVE_ERROR.getCode(),FundExceptionCode.REDIS_SAVE_ERROR.getMsg());
        return new Result().setCode(200).setMsg("更新场馆信息成功");
    }

    @Override
    public Result renewCoachInfo(CoachToFund cf) throws JsonProcessingException {
        if(!pns.saveCoachInfo(BeanCopyUtil.copyObject(cf, CoachToParam.class)))
            throw new MyException(FundExceptionCode.REDIS_SAVE_ERROR.getCode(),FundExceptionCode.REDIS_SAVE_ERROR.getMsg());
        return new Result().setCode(200).setMsg("储存教练信息成功");
    }

    @Override
    public Result deleteCoashInfo(Integer cid) throws JsonProcessingException {
        pns.deleteCoashInfo(cid);
        return new Result().setCode(200).setMsg("删除教练信息成功");
    }

    @Override
    public Result deleteVenueInfo(Integer vid) throws JsonProcessingException {
        //删除地理位置接口
        rt.boundZSetOps(KEY).remove(vid+"");
        pns.deleteVenueInfo(vid);
        return new Result().setCode(200).setMsg("删除场馆信息成功");
    }

    //改

}
